package com.cailu.rabmq.sender.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cailu.common.constant.SenderConfTopicCommon;

/**
 * @author cailu
 * @filename SenderConfTopic
 * @description Topic转发模式
 * @date 2020/3/31 15:32
 */
@Configuration
public class SenderConfTopicConfig {

	@Bean
	public Queue queueMessage() {
		return new Queue(SenderConfTopicCommon.DIRECT_TOPIC_MESSAGE);
	}

	@Bean(name = SenderConfTopicCommon.TOPIC_MESSAGES)
	public Queue queueMessages() {
		return new Queue(SenderConfTopicCommon.DIRECT_TOPIC_TO_MESSAGE);
	}

	@Bean
	public TopicExchange exchange() {
		return new TopicExchange(SenderConfTopicCommon.DIRECT_TOPIC_MESSAGE_EXCHANGE);
	}


	@Bean
	Binding bindingExchangeMessage() {
		return BindingBuilder.bind(queueMessage()).to(exchange()).with(SenderConfTopicCommon.DIRECT_TOPIC_MESSAGE);
	}

	/**
	 * 说明：绑定交换机
	 * *（星号）：可以（只能）匹配一个单词
	 * #（井号）：可以匹配多个单词（或者零个）
	 * Title: bindingExchangeMessages
	 * @param queue 交换机
	 * @param exchange      需要绑定的交换机
	 * @return
	 */
	@Bean
	Binding bindingExchangeMessages(@Qualifier(SenderConfTopicCommon.TOPIC_MESSAGES) Queue queue,
	                                TopicExchange exchange) {
		// *表示一个词,#表示零个或多个词*.email  *.user
		//由queueMessages绑定exchange 交换机，与【 topic.#】规则匹配的 全部消息都路由到exchange上
		//例如 topic.# 能够匹配到“topic.irs.corporate” 但是“topic.*” 只会匹配到“topic.irs”
		return BindingBuilder.bind(queue).to(exchange).with("topic.#");
	}
}
